package com.thp.bigdata.thread.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyInterruptibly {
	
	private Lock lock = new ReentrantLock();
	
	public static void main(String[] args) {
		MyInterruptibly test = new MyInterruptibly();
        MyThread thread0 = new MyThread(test);
        MyThread thread1 = new MyThread(test);
        thread0.start();
        thread1.start();
        
        try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
        
        // 等待2秒中  线程2 就会被中断
        thread1.interrupt();
        System.out.println("==============================-");
        
        
	}
	
	
	
	public void insert(Thread thread) throws InterruptedException {
		lock.lockInterruptibly();
		try {
			System.out.println(thread.getName() + "得到了锁");
			long satrtTime = System.currentTimeMillis();
			for( ; ; ) {
				if(System.currentTimeMillis() - satrtTime >= Integer.MAX_VALUE) {
					break;
				}
			}
		} catch (Exception e) {
			
		} finally {
			System.out.println(Thread.currentThread().getName() + "执行finally");
			lock.unlock();
			System.out.println(thread.getName() + "释放了锁");
		}
	}
	
}

class MyThread extends Thread {
	private MyInterruptibly test = null;
	
	public MyThread(MyInterruptibly test) {
		this.test = test;
	}
	@Override
	public void run() {
		try {
			test.insert(Thread.currentThread());
		} catch (Exception e) {
			System.out.println(Thread.currentThread().getName() + "被中断");
		}
	}
	
}

